<?php

class MemberAction extends PublicAction {

    public $db,$memberGroup;

    function _initialize()
    {
        parent::_initialize();
        $this->memberGroup = M('MemberGroup')->select();

        $this->assign('memberGroup',$this->memberGroup);
    }


    function index()
    {
        import('@.ORG.Page');

        $keyword    = $_GET['keyword'];
        $searchtype = $_GET['searchtype'];
        $groupid    = intval($_GET['groupid']);

        $this->assign($_GET);

        if (!empty($keyword) && !empty($searchtype)) {
          $where[$searchtype]=array('like','%'.$keyword.'%');
        }
        if ($groupid) {
            $where['groupid']=$groupid;
        }

        $model = D('Member');
        $count = $model->where($where)->count();
        $page = new Page($count,20);
        $show = $page->show();
        $this->assign("page",$show);

        $list = $model->relation('MemberGroup')->order('id')->where($where)
        ->limit($page->firstRow.','.$page->listRows)->select();

        $this->assign('ulist',$list);
        $this->display();
    }

    function _before_edit()
    {
        $this->assign('rlist',$this->memberGroup);
    }


    /**
     * 更新
     *
     */
    function edit()
    {

        $model = M('Member');
        $id = $_GET['id'];

        if(empty($id))
          $this->error(L('do_empty'));

        $member = $model->find($id);

        $membergroup = M('MemberGroup')->select();

        $this->assign('member', $member);
        $this->assign('membergroup', $membergroup);

        $this->display();
    }


    function update()
    {

        $model = M('Member');
        $_POST['password'] = $_POST['password'] ? sysmd5($_POST['password']) : $_POST['opassword'];

        if($data = $model->create()){
            if (!empty($data['id'])) {
                if (false !== $model->save()) {
                    $this->redirect('index');
                } else {
                  $this->error(L('edit_error').$model->getDbError());
                }
            } else {
                $this->error(L('do_error'));
            }
        } else {
          $this->error($model->getError());
        }
    }


    function _before_add(){
        $this->assign('rlist',$this->memberGroup);
    }

    function add(){
        $membergroup = M('MemberGroup')->select();

        $this->assign('membergroup', $membergroup);
        $this->display();
    }


    function insert(){
        $model = M('Member');

        $_POST['password'] = sysmd5($_POST['password']);

        if ($data = $model->create()) {
            if(false!==$model->add()){
                $uid = $model->getLastInsID();
                $ru['role_id'] = $_POST['groupid'];
                $ru['user_id'] = $uid;
                $roleuser = M('RoleUser');
                $roleuser->add($ru);
                $this->success(L('add_ok'));
            } else {
                $this->error(L('add_error'));
            }
        }else{
            $this->error($model->getError());
        }
    }

    function delete(){
        $id = $_GET['id'];
        $user = D('Member');
        if (false!==$user->delete($id)) {
            $roleuser = M('RoleUser');
            $roleuser->where('user_id ='.$id)->delete();
            delattach(array('moduleid'=>0,'catid'=>0,'id'=>0,'userid'=>$id));
            $this->success(L('delete_ok'));
        } else {
            $this->error(L('delete_error').$user->getDbError());
        }
    }

    function deleteall()
    {
        $ids = $_POST['ids'];
        if (!empty($ids) && is_array($ids)) {
            $user = D('Member');
            $id=implode(',',$ids);
            if (false!==$user->delete($id)) {
                $roleuser = M('RoleUser');
                $roleuser->where('user_id in('.$id.')')->delete();
                delattach("moduleid=0 and catid=0 and id=0 and userid in($id)");
                $this->success(L('delete_ok'));
            } else {
                $this->error(L('delete_error'));
            }
        }else{
          $this->error(L('do_empty'));
        }
    }
}